/*
 * @Descripttion: redux-toolkit的configureStore模块
 * @Author: lukasavage
 * @Date: 2021-12-06 21:43:27
 * @LastEditors: lukasavage
 * @LastEditTime: 2021-12-07 19:59:59
 */


import { createStore, applyMiddleware, combineReducers, compose } from 'redux';
import thunk from 'redux-thunk';


// 这个方法是toolkit必须的，没有的话会报错
function isPlainObject(value) {
    if (typeof value !== "object" || value === null)
        return false;
    return Object.getPrototypeOf(value) === Object.prototype;
}
function configureStore(options = {}) {
    // redux-toolkit内置thunk中间件
    let { reducer, middleware = [thunk], preloadedState } = options;
    let rootReducer;
    if (typeof reducer === "function") {
        rootReducer = reducer;
    } else if (isPlainObject(reducer)) {          // 有可能传递过来一个对象，我们只需要将它用combineReducers包裹起来就好
        rootReducer = combineReducers(reducer);
    }
    const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose; 
    return createStore(rootReducer, preloadedState, composeEnhancers(applyMiddleware(...middleware)));
}
export default configureStore;